//
//  SecureViewController.m
//  DaDaBao
//
//  Created by admin on 15/8/18.
//  Copyright (c) 2015年 User. All rights reserved.
//
#import "SecureViewController.h"
#import "RealNameViewController.h"
#import "PayPwdViewController.h"
#import "MemberRealNameViewController.h"

@interface SecureViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property(nonatomic,strong)UITableView *secureTable;
@property(nonatomic,strong)NSArray *secureArr;
@property(nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation SecureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    [self createUI];
    if (!_secureArr) {
        _secureArr =@[@"手机认证",@"实名认证",@"支付密码"];
    }
    if (!_secureArr) {
        _secureArr = [NSMutableArray array];
    }
    
    if(!_dataArr)
    {
        _dataArr = [NSMutableArray array];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSString *safeSet = ([[[NSUserDefaults standardUserDefaults]objectForKey:@"idCardVStatus"] isEqualToNumber:[NSNumber numberWithInt:1]]) ? @"已设置" : @"未设置";
    NSString *payPasswordSet = ([[[NSUserDefaults standardUserDefaults]objectForKey:@"payPasswordVStatus"] isEqualToNumber:[NSNumber numberWithInt:1]]) ? @"已设置" : @"未设置";
    [_dataArr insertObject:@"已设置" atIndex:0];
    [_dataArr insertObject:safeSet atIndex:1];
    [_dataArr insertObject:payPasswordSet atIndex:2];
    [_secureTable reloadData];
}

-(void)createUI
{
    TopView *topView =[[TopView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    [topView setTitile:@"安全设置"];
    [self.view addSubview:topView];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, 20.0f, 64.0f, 44.0f)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
    
    if (!_secureTable) {
        _secureTable = [[UITableView alloc] initWithFrame:CGRectMake(0.0f, topView.frame.size.height+topView.frame.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT-64.0f) style:UITableViewStylePlain];
        _secureTable.delegate = self;
        _secureTable.dataSource = self;
        _secureTable.separatorStyle = UITableViewCellSeparatorStyleNone;
        _secureTable.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
        
        [self.view addSubview:_secureTable];

    }
}
#pragma mark--UITableViewDelegate&DataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        UILabel *sepLab = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 43.5f, SCREEN_WIDTH, 0.5f)];
        sepLab.backgroundColor = [UIColor lightGrayColor];
        [cell addSubview:sepLab];

    }
    cell.textLabel.text = [self.secureArr objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [self.dataArr objectAtIndex:indexPath.row];
    if ([cell.detailTextLabel.text isEqualToString:@"未设置"])
    {
        cell.detailTextLabel.textColor = [UIColor redColor];
    }else
    {
        cell.detailTextLabel.textColor = [UIColor lightGrayColor];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.secureArr.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.row) {
        case 0:
        {
//     去 手机认证
            ALERT_VIEW(@"已进行手机认证");
        }
            break;
        case 1:
        {
//    去实名认证
          if([[[NSUserDefaults standardUserDefaults] objectForKey:@"idCardVStatus"] isEqualToNumber:[NSNumber numberWithInt:1]])
          {
              ALERT_VIEW(@"已进行实名认证");
          }else
          {
            UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"实名认证身份选择" delegate:self cancelButtonTitle:@"内部员工" otherButtonTitles:@"外部客户", nil];
            alert.tag = 2000;
            [alert show];
          }
        }
            break;
        case 2:
        {
//   支付密码
            PayPwdViewController *payVC = [[PayPwdViewController alloc] init];
            [self.navigationController pushViewController:payVC animated:TRUE];
        }
            break;
        default:
            break;
    }
}
#pragma mark-返回上一页
-(void)buttonBackTapped:(id)sender
{
    [self.navigationController popViewControllerAnimated:TRUE];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSLog(@"内部");
        MemberRealNameViewController *realVC = [[MemberRealNameViewController alloc] init];
        [self.navigationController pushViewController:realVC animated:TRUE];
    }else
    {
        NSLog(@"外部");
        RealNameViewController *realVC = [[RealNameViewController alloc] init];
        [self.navigationController pushViewController:realVC animated:TRUE];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
